package hk.m4s.lr.repair.test.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.common.Constant;
import framework.common.alertview.AlertView;
import framework.common.alertview.OnItemClickListener;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.DeviceModelResult;
import hk.m4s.lr.repair.test.model.PushModel;
import hk.m4s.lr.repair.test.model.PushModelResult;
import hk.m4s.lr.repair.test.service.device.DeviceListervice;
import hk.m4s.lr.repair.test.service.notice.NoticeService;
import hk.m4s.lr.repair.test.ui.adapter.PushListAdapter;
import hk.m4s.lr.repair.test.views.listview.InnerListView;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/3/23.
 * 设置
 * meaagePush=1;//信息推送打开为1,反之0
 * xitongPush=1;系统通知 打开为1,反之0
 * yewuPush=0;业务打开为1,反之0
 */

public class AppSetActivity extends UeBaseActivity implements View.OnClickListener, OnItemClickListener {
    private RelativeLayout click_count, click_exit,click_yijian;
    private LinearLayout click_clear;
    private InnerListView push_listview;
    private PushListAdapter pushListAdapter;
    private List<PushModel> pushModelList = new ArrayList<>();
    private int selectType=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_app_set);
        hiddenFooter();
        hiddenNewMessage();
        showGoBackBtn();
        setTitleText("设置");
        findBaseView();
    }

    public void findBaseView() {
        push_listview = (InnerListView) findViewById(R.id.push_listview);
        click_count = (RelativeLayout) findViewById(R.id.click_count);
        click_exit = (RelativeLayout) findViewById(R.id.click_exit);
        click_clear = (LinearLayout) findViewById(R.id.click_clear);
        click_yijian= (RelativeLayout) findViewById(R.id.click_yijian);
        click_exit.setOnClickListener(this);
        click_clear.setOnClickListener(this);
        click_count.setOnClickListener(this);
        click_yijian.setOnClickListener(this);
        pushListAdapter = new PushListAdapter(AppSetActivity.this, pushModelList);
        push_listview.setAdapter(pushListAdapter);
        getData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.click_count:
                Intent intent = new Intent(AppSetActivity.this, UserNameSetActivity.class);
                startActivity(intent);
                break;
            case R.id.click_clear:
                selectType=1;
                new AlertView("提示", "是否要清除缓存", "取消", new String[]{"确定"}, null, this, AlertView.Style.Alert, this).setCancelable(true).show();
                break;
            case R.id.click_exit:
                selectType=2;
                new AlertView("提示", "是否要退出?", "取消", new String[]{"确定"}, null, this, AlertView.Style.Alert, this).setCancelable(true).show();
                break;
            case R.id.click_yijian:
                Intent intents = new Intent(AppSetActivity.this, AppYiJianActivity.class);
                startActivity(intents);
                break;
        }
    }

    @Override
    public void onItemClick(Object o, int position) {
        //判断是否是拓展窗口View，而且点击的是非取消按钮
        if (position == -1) {
            return;
        } else {
            if (position == 0) {
                if(selectType==1){
                    SharedPreferencesUtils.addgetSharedPreferences("proint", "");
                    SharedPreferencesUtils.addgetSharedPreferences("token", "");
                }else {
                    SharedPreferencesUtils.addgetSharedPreferences(Constant.userId, "");
                    Intent mIntent = new Intent("hk.m4s.lr.repair.test_finish");
                    // 发送广播
                    sendBroadcast(mIntent);
                }
            }
        }
    }

    public void getData() {
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("device_type", Constant.device_type);
        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
        JSONObject jsondata = new JSONObject();
        parmas.put("jsonText", jsondata.toString());
        NoticeService.userPushSetList(AppSetActivity.this, parmas, new ResponseCallback<PushModelResult>() {
            @Override
            public void onSuccess(PushModelResult deviceModelResult) {
                pushModelList.clear();
                pushModelList.addAll(deviceModelResult.getList());
                pushListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }
}
